package com.shakespace.artofandroid.global

import android.annotation.SuppressLint
import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Build
import java.lang.reflect.InvocationTargetException


object Utils {

    fun getProcessName(context: Context): String {
        var currentProcName = ""
        val pid = android.os.Process.myPid()
        val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        for (processInfo in manager.runningAppProcesses) {
            if (processInfo.pid == pid) {
                currentProcName = processInfo.processName
            }
        }
        return currentProcName
    }

    // 2020-01-07 NOTE: another way to get process name
    fun getNameFromActivityThread(): String {
        return if (Build.VERSION.SDK_INT >= 28)
            Application.getProcessName()
        else {
            // Using the same technique as Application.getProcessName() for older devices
            // Using reflection since ActivityThread is an internal API

            try {
                @SuppressLint("PrivateApi")
                val activityThread = Class.forName("android.app.ActivityThread")

                // Before API 18, the method was incorrectly named "currentPackageName", but it still returned the process name
                // See https://github.com/aosp-mirror/platform_frameworks_base/commit/b57a50bd16ce25db441da5c1b63d48721bb90687
                val methodName = if (Build.VERSION.SDK_INT >= 18) "currentProcessName" else "currentPackageName"

                val getProcessName = activityThread.getDeclaredMethod(methodName)
                getProcessName.invoke(null) as String
            } catch (e: ClassNotFoundException) {
                throw RuntimeException(e)
            } catch (e: NoSuchMethodException) {
                throw RuntimeException(e)
            } catch (e: IllegalAccessException) {
                throw RuntimeException(e)
            } catch (e: InvocationTargetException) {
                throw RuntimeException(e)
            }

        }
    }

}